

#include "LightThread.h"


void LightThread::rot(bool on, int period)
{
	onRot = on;
	periodRot = period;
}

void LightThread::gruen(bool on, int period)
{
	onGruen = on;
	periodGruen = period;
}

void LightThread::gelb(bool on, int period)
{
	onGelb = on;
	periodGelb = period;
}

void * LightThread::runGruen(void *arg)
{
	LightThread * inst = (LightThread *)arg;
	int actOn = 0;
	while(true)
	{
		if(inst->periodGruen != 0)
		{
			if(!inst->onGruen)
			{
				HWaccess::getInstance()->set(AMPEL_GRUN, 0);
				usleep(50000);
			}
			else
			{
				actOn = ((actOn==1)?0:1);
				HWaccess::getInstance()->set(AMPEL_GRUN, actOn);
				usleep((inst->periodGruen/2)*1000);
			}
		}
		else
		{
			actOn = inst->onGruen?1:0;
			HWaccess::getInstance()->set(AMPEL_GRUN, actOn);
			usleep(50000);
		}
	}
	return 0;
}

void * LightThread::runRot(void *arg)
{
	LightThread * inst = (LightThread *)arg;
	int actOn = 0;
	while(true)
	{
		if(inst->periodRot != 0)
		{
			if(!inst->onRot)
			{
				HWaccess::getInstance()->set(AMPEL_ROT, 0);
				usleep(50000);
			}
			else
			{
				actOn = ((actOn==1)?0:1);
				HWaccess::getInstance()->set(AMPEL_ROT, actOn);
				usleep((inst->periodRot/2)*1000);
			}
		}
		else
		{
			actOn = inst->onRot?1:0;
			HWaccess::getInstance()->set(AMPEL_ROT, actOn);
			usleep(50000);
		}
	}
	return 0;
}

void * LightThread::runGelb(void *arg)
{
	LightThread * inst = (LightThread *)arg;
	int actOn = 0;
	while(true)
	{
		if(inst->periodGelb != 0)
		{
			if(!inst->onGelb)
			{
				HWaccess::getInstance()->set(AMPEL_GELB, 0);
				actOn = 0;
				usleep(50000);
			}
			else
			{
				actOn = ((actOn==1)?0:1);
				HWaccess::getInstance()->set(AMPEL_GELB, actOn);
				usleep((inst->periodGelb/2)*1000);
			}
		}
		else
		{
			actOn = inst->onGelb?1:0;
			HWaccess::getInstance()->set(AMPEL_GELB, actOn);
			usleep(50000);
		}
	}
	return 0;
}

LightThread::~LightThread()
{
	ThreadDestroy(threadRot, 0, 0);
	ThreadDestroy(threadGelb, 0, 0);
	ThreadDestroy(threadGruen, 0, 0);
}

void LightThread::allOff()
{
}

LightThread::LightThread()
{
	onRot = false;
	onGelb = false;
	onGruen = false;
	periodRot = 0;
	periodGelb = 0;
	periodGruen = 0;

	pthread_create(&threadRot, NULL, runRot, this);
	pthread_create(&threadGelb, NULL, runGelb, this);
	pthread_create(&threadGruen, NULL, runGruen, this);
}


